package com.xuln.framework.action;

import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.xuln.common.config.SystemConfig;
import com.xuln.common.config.SystemConfigUtil;

public class BaseAction extends ActionSupport {
	private static final long serialVersionUID = 523074217307301509L;
	protected Logger log = LoggerFactory.getLogger(this.getClass());

	/** 得到用户选择的语言，默认为浏览器语言 */
	protected Locale getUserLocale() {
		if (getSession("locale") == null) {
			setSession("locale", getLocale());
		}
		return (Locale) getSession("locale");
	}

	/** 获取Attribute */
	public Object getAttribute(String name) {
		return ServletActionContext.getRequest().getAttribute(name);
	}

	/** 设置Attribute */
	public void setAttribute(String name, Object value) {
		ServletActionContext.getRequest().setAttribute(name, value);
	}

	/** 获取Parameter */
	public String getParameter(String name) {
		return getRequest().getParameter(name);
	}

	/** 获取Parameter数组 */
	public String[] getParameterValues(String name) {
		return getRequest().getParameterValues(name);
	}

	/** 获取Session */
	public Object getSession(String name) {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		return session.get(name);
	}

	/** 获取Session */
	public Map<String, Object> getSession() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		return session;
	}

	/** 设置Session */
	public void setSession(String name, Object value) {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		session.put(name, value);
	}

	/** 获取Request */
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/** 获取Response */
	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/** 获取Application */
	public ServletContext getApplication() {
		return ServletActionContext.getServletContext();
	}

	/** 获取系统配置信息 */
	public SystemConfig getSystemConfig() {
		return SystemConfigUtil.getSystemConfig();
	}
}
